package com.museum.data;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.museum.model.Location;
import com.museum.model.Mp_Places_Museum;
import com.museum.model.Open_Date;
import com.museum.model.Open_Date_Item;
import com.museum.model.Place;

public class Mp_Places_Museum_Handler {
	private Mp_Places_Museum mp_places_museum;
	private Place place;
	
	private String _place_id;
	
	XmlRpc xmlrpc = new XmlRpc();
	public Mp_Places_Museum_Handler(String place_id){
		_place_id = place_id;
		mp_places_museum = new Mp_Places_Museum();
		this.Parse();
		
	}
	public Mp_Places_Museum getMp_Places_Museum(){		
		return mp_places_museum;
	}
	private void Parse(){		
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("place_id", _place_id);
      	
      	Map<String, Object> response = xmlrpc.call("mp.places.museum", params);
      	place = new Place();
      	for (Map.Entry<String, Object> entry3 : response.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            //Log.d("TAG","value3: " + value3.toString());
            
            if(key3.equals("place_id")){
            	place.setPlaceId(value3.toString());
            }
            else if(key3.equals("email")){
            	place.setEmail(value3.toString());
            }else if(key3.equals("website")){
            	place.setWebsite(value3.toString());
            }else if(key3.equals("added_timestamp")){
            	place.setAdded_timestamp(value3.toString());
            }else if(key3.equals("description")){
            	place.setDescription(value3.toString());
            }else if(key3.equals("name")){
            	place.setName(value3.toString());
	      	}else if(key3.equals("create_timestamp")){
	      		place.setCreate_timestamp(value3.toString());
	        }else if(key3.equals("exhibition_status")){
	        	place.setExhibition_status(value3.toString());
	        }
	        else if(key3.equals("location")){
	        	Location location = new Location();
	            Map<String, Object> response4 = (Map<String, Object>)value3;
			    for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			         String key4 = entry4.getKey();
				     Object value4 = entry4.getValue();								         
				        
				     if(key4.equals("longitude")){
				    	 location.setLongitude(value4.toString());
				     }else if(key4.equals("description")){
				        location.setDescription(value4.toString());
				     }else if(key4.equals("latitude")){
				        location.setLatitude(value4.toString());
				     }								        
			    }//End Loop4
			    place.setLocation(location);
	        }else if(key3.equals("open_date")){
	        	Open_Date opendate = new Open_Date();
	        	Map<String, Object> response4 = (Map<String, Object>)value3;
			        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			           	String key4 = entry4.getKey();
				        Object value4 = entry4.getValue();
				        
				        Open_Date_Item opendateitem = new Open_Date_Item();
				        opendateitem.setDate(key4);
					    Map<String, Object> response5 = (Map<String, Object>)value4;
						for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
							String key5 = entry5.getKey();
							Object value5 = entry5.getValue();									        
								        
							if(key5.equals("close_time")){
								opendateitem.setClose_time(value5.toString());
							}else if(key5.equals("open_time")){
								opendateitem.setOpen_time(value5.toString());
							}							
						}//End Loop5
						opendate.addItem(opendateitem);
						place.setOpen_date(opendate);
			        }//End Loop4
	          }//End if
        }//End Loop3
        mp_places_museum.addItem(place);
	}
}
